import React from "react";
import { Button, Form, Input, Card } from "antd";

import styles from "@/assets/styles/modules/login.module.scss";

import { changeLoginState } from "@/store/modules/loginStore";

import { useAppDispatch } from "@/store/hook.ts";

type FieldType = {
  username?: string;
  password?: string;
  remember?: string;
};

const Login: React.FC = () => {
  const dispatch = useAppDispatch();

  const onFinish = (values: any) => {
    console.log("Success:", values);
    if (values.username === "admin" && values.password === "admin") {
      dispatch(changeLoginState(true));
      localStorage.setItem("blogToken", "admin");
    }
  };

  const onFinishFailed = (errorInfo: any) => {
    console.log("Failed:", errorInfo);
  };

  return (
    <div className={styles.main}>
      <Card title="登 录 系 统" bordered={false} style={{ width: 600 }}>
        <Form
          name="basic"
          wrapperCol={{ span: 18 }}
          style={{ maxWidth: 600 }}
          initialValues={{ remember: true }}
          onFinish={onFinish}
          onFinishFailed={onFinishFailed}
          layout="vertical"
          autoComplete="off"
        >
          <Form.Item<FieldType>
            label="用 户"
            name="username"
            rules={[{ required: true, message: "Please input your username!" }]}
          >
            <Input />
          </Form.Item>

          <Form.Item<FieldType>
            label="密  码"
            name="password"
            rules={[{ required: true, message: "Please input your password!" }]}
          >
            <Input.Password />
          </Form.Item>

          <Form.Item>
            <Button type="primary" htmlType="submit">
              登 录
            </Button>
          </Form.Item>
        </Form>
      </Card>
    </div>
  );
};
export default Login;
